package com.jzr.board.presenter.impl

import android.content.Context
import com.jzr.board.api.Api
import com.jzr.board.base.BasePresenter
import com.jzr.board.bean.*
import com.jzr.board.bean.body.RecordBody
import com.jzr.board.presenter.contract.SettingActivityContract
import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class SettingActivityImpl(context: Context) : BasePresenter<SettingActivityContract.View>(),
    SettingActivityContract.Presenter<SettingActivityContract.View> {

    private var context: Context? = null

    init {
        this.context = context
    }


    override fun connectTest() {
        if (context != null) {
            showLoadingDialog(context!!)
        }
        addSubscrebe(
            Api.getInstance()?.connectTest()
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<ConnectTestBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError("连接失败")
                    }

                    override fun onNext(t: ConnectTestBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null) {
                            mView?.connectTestSuccess(t)
                        } else {
                            mView?.showError("连接失败")
                        }
                    }
                })
        )
    }

    override fun getBindingDeptNo(s: RecordBody?) {
        if (context != null) {
            showLoadingDialog(context!!)
        }

        addSubscrebe(
            Api.getInstance()?.getBindingDeptNo(s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<BindingDeptBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: BindingDeptBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getBindingDeptNoSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }
//
//    override fun getCheckBedNo(vararg s: String?) {
//        if (context != null) {
//            showLoadingDialog(context!!)
//        }
//        addSubscrebe(
//            Api.getInstance()?.getCheckBedNo(*s)
//                ?.observeOn(AndroidSchedulers.mainThread())
//                ?.subscribeOn(Schedulers.io())
//                ?.unsubscribeOn(Schedulers.io())
//                ?.subscribe(object : Observer<CheckBedListBean?> {
//                    override fun onCompleted() {}
//                    override fun onError(e: Throwable) {
//                        dismissLoadingDialog()
//                        if (mView != null) mView?.showError(e.toString())
//                    }
//
//                    override fun onNext(t: CheckBedListBean?) {
//                        dismissLoadingDialog()
//                        if (mView != null && t != null) {
//                            mView?.getCheckBedNoSuccess(t)
//                        } else {
//                            mView?.showError(t?.msg)
//                        }
//                    }
//                })
//        )
//    }
//
//
    override fun getCheckDeptNo() {
        if (context != null) {
            showLoadingDialog(context!!)
        }
        addSubscrebe(
            Api.getInstance()?.getCheckDeptNo()
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DeptCodeListBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: DeptCodeListBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getCheckDeptNoSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }
//
    override fun getDeviceInfo(vararg s: String?) {
        if (context != null) {
            showLoadingDialog(context!!)
        }
        addSubscrebe(
            Api.getInstance()?.getDeviceInfo(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DeviceInfoBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: DeviceInfoBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getDeviceInfoSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }

    override fun getLastVersion(context: Context?, vararg s: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getLastVersion(*s)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<LastVersionBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: LastVersionBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getLastVersionSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }
}